// 远程路由方案时关于用户的mock请求
import mockRequest from "./request"
import useUserList from "@/views/sysAuth/user/userList"
import useRoleList from "@/views/sysAuth/role/roleList"
import useRouteList from "@/views/sysAuth/route/routeList"
import useRequestList from "@/views/sysAuth/request/requestList"
import {authRouteMode,PRIVATE_KEY} from "@/common/config"
import md5 from 'blueimp-md5'

export default {
  getUserInfoByAccount(accountInfo:{userName:string,password:string}){
    if(authRouteMode=='local')  
    return ({
      code:200,
      msg:'success',
      info:{token:'adadasda',userName:'admin'} ,
    })
   const userList=useUserList()
   return mockRequest(()=>{
    const item=userList.list.find(v=>accountInfo.userName===v.userName && accountInfo.password===v.password)
    if(item)
    {  
      const {roleIds}=item
      const token=encodeURIComponent(JSON.stringify({roleIds}))
      return ({
        code:200,
        msg:'success',
        info:{...item,id:md5(item.id,PRIVATE_KEY),token} ,
      })
    }
    else return({
      code:201,
      msg:'userName or password error',
    })
    },false)
  },
  getUserInfoByPhone(info:{phone:string,verifyCode:string}){
    if(authRouteMode=='local')  
    return ({
      code:200,
      msg:'success',
      info:{token:'adadasda',userName:'admin'} ,
    })
    const userList=useUserList()
    return mockRequest(()=>{
     const item=userList.list.find(v=>info.phone===v.phone)
     if(item)
     {  
       const {roleIds}=item
       const token=encodeURIComponent(JSON.stringify({roleIds}))
       return ({
         code:200,
         msg:'success',
         info:{...item,token} ,
       })
     }
     else return({
       code:201,
       msg:'phone error',
     })
     },false)
   },
  getRolesInfo(token:string){
    return mockRequest(()=>{
     let roleIds=''
       try {
         roleIds=JSON.parse(decodeURIComponent(token)).roleIds
       } catch (error) {}
       if(roleIds){
         const roleList=useRoleList()
         const routeList=useRouteList()
         const requestList=useRequestList()
         const hasRoleList:RoleItem[]=JSON.parse(JSON.stringify(roleList.list.filter(v=>roleIds.includes(v.id as string))))
         if(!hasRoleList.length) 
         return({
          code:201,
          message:'error token'
        })
         let routeIds:string[]=[]
         const routes:routeTreeNode[]=[]
         let accessRequestIds:string[]=[]
         const accessRequestNameList:string[]=[]
         const _executeActionList:{routeId:string,actionList:string[]}[]=[] 
         hasRoleList.forEach(v=>{
            routeIds.push(...v.routeIds.split(','))
            accessRequestIds.push(...v.accessRequestIds.split(","))
            
            let executeAction:objType={}
            try {
              executeAction=JSON.parse(v.executeAction)
            } catch (error) {}
            Object.keys(executeAction).forEach(k=>{
             _executeActionList.push({routeId:k,actionList:executeAction[k].split(',')})
            })
 
         })
          // 追加无需授权的路由
         routeList.list.forEach(v=>{
             if(v.noNeedAuth) routeIds.push(v.id as string)
         })
         routeIds=[...new Set(routeIds)]
         JSON.parse(JSON.stringify(routeList.list)).forEach((v:routeTreeNode)=>{
             if(routeIds.includes(v.id as string)) routes.push(v)
         })
         accessRequestIds=[...new Set(accessRequestIds)]
         requestList.list.forEach(v=>{
           if(accessRequestIds.includes(v.id as string))  accessRequestNameList.push(v.requestName)
         })
 
         const executeActionListMap:{[key:string]:{routeId:string,actionList:string[]}}={}
         _executeActionList.forEach(v=>{
           if(!executeActionListMap[v.routeId]) executeActionListMap[v.routeId]=v
           else{
             executeActionListMap[v.routeId].actionList=[...new Set([...executeActionListMap[v.routeId].actionList,...v.actionList])]
           }
         })
         routes.forEach(v=>{
           if(executeActionListMap[v.id]){
             v.actionList=executeActionListMap[v.id].actionList
           }
           else v.actionList=[]
         })
         return({
           code:200,
           msg:'success',
           routes,
           accessRequestNameList,
         })
       }
         
         else return({
           code:201,
           message:'error token'
         })
    })
   },
}






